/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.classicmodels.model;

import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Chona
 */
public class CustomerDAO {  
    public static Customer[] all() throws Exception {
             Connection c=Datasource.getConnection();
             Statement s=  (Statement) c.createStatement();
             ResultSet rs = s.executeQuery("select * from Customers");
      
             ArrayList <Customer> customers=new ArrayList <Customer>();
             
             while (rs.next())
             {
                 Customer customer=new Customer();
                    
                 customer.setCustomerNumber(rs.getInt("customerNumber"));
                 customer.setCustomerName(rs.getString("customerName"));
                 customer.setPhone(rs.getString("phone"));
                 customer.setCustomerFirstName(rs.getString("contactFirstName"));
                 customer.setCustomerLastName(rs.getString("contactLastName"));
                 customer.setAdressline1(rs.getString("addressLine1"));
                 customer.setAdressline2(rs.getString("addressLine2"));
                 customer.setCity(rs.getString("city"));                   
                 customer.setState(rs.getString("state"));
                 customer.setPostalCode(rs.getString("postalCode"));
                 customer.setCountry(rs.getString("country"));
                 customer.setSalesRepEmployeeNumber(rs.getInt("salesRepEmployeeNumber"));
                 customer.setCreditLimit(rs.getInt("creditLimit"));
                 
                 customers.add(customer);
             }
             Customer[] customersArray =new Customer [customers.size()];
             
             customers.toArray(customersArray);
             
             return customersArray;
             
             //1) AGREGAR EL METODO SAVE () AL CUSTOMERDAO
             //PUBLIC STATIC VOID SAVE (CUSTOMER C)
             
             //2) MOSTRAR EN EL CUSTOMERLIST (PANTALLA) EL ARRAY DE CUSTOMER QUE LE PASO POR PARAMETRO
    }
    
    public static void Save_Customer (Customer customer) throws Exception {
        try {   
             Connection con=Datasource.getConnection();
            
             PreparedStatement s2=(PreparedStatement) con.prepareStatement("INSERT INTO customers values (?,?,?,?,?,?,?,?,?,?,?,?,?);"); 
	
              System.out.println(customer.getCustomerNumber());
              
	     s2.setInt(1,customer.getCustomerNumber());
             s2.setString(2,customer.getCustomerName());
             s2.setString(3,customer.getCustomerLastName());
             s2.setString(4,customer.getCustomerFirstName());
             s2.setString(5,customer.getPhone());
             s2.setString(6,customer.getAdressline1());
             s2.setString(7,customer.getAdressline2());
	     //s2.setString(7,customer.getCity());
             s2.setString(8,"Don Torcuato");
             s2.setString(9,customer.getState());
             s2.setString(10,customer.getPostalCode());
             s2.setString(11,customer.getCountry());
             s2.setInt(12,customer.getSalesRepEmployeeNumber());
             s2.setInt(13,customer.getCreditLimit());
                
             s2.executeUpdate();
                     
        } catch (Exception ex) {
            Logger.getLogger(CustomerDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
                 
        
        
        
    }
}
